//
//  MM_HomeSearchHeaderView.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/11/1.
//

#import "MM_HomeSearchHeaderView.h"

@interface MM_HomeSearchHeaderView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *searchView_1;
@property (nonatomic, strong) UIImageView *searchImageView_1;
@property (nonatomic, strong) UITextField *goodsTextField;
@property (nonatomic, strong) UITextField *shippingTextField;
@property (nonatomic, strong) UIButton *searchBtn;

@property (nonatomic, strong) UIView *searchView_2;
@property (nonatomic, strong) UIImageView *searchImageView_2;
@property (nonatomic, strong) UITextField *oldCodeTextField;
@property (nonatomic, strong) UIButton *oldSearchBtn;

@end

@implementation MM_HomeSearchHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = MM_ColorUtil(@"#ffffff");
        
        _searchView_1 = [[UIView alloc] init];
        _searchView_1.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        _searchView_1.layer.cornerRadius = 5;
        _searchView_1.layer.borderWidth = 1;
        _searchView_1.layer.borderColor = MM_ColorUtil(@"#dddddd").CGColor;
        [self addSubview:_searchView_1];
        
        _searchImageView_1 = [[UIImageView alloc] init];
        _searchImageView_1.image = [UIImage imageNamed:@"searchHomeImage"];
        [self.searchView_1 addSubview:_searchImageView_1];
        
        _goodsTextField = [[UITextField alloc] init];
        _goodsTextField.delegate = self;
        _goodsTextField.textAlignment = NSTextAlignmentLeft;
        [_goodsTextField addTarget:self action:@selector(goodsTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        _goodsTextField.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        _goodsTextField.keyboardType = UIKeyboardTypeDefault;
        _goodsTextField.placeholder = @"输入商品名称、规格名称、SKUID";
        _goodsTextField.clearButtonMode = UITextFieldViewModeAlways;
        NSMutableDictionary *attDic = [@{NSForegroundColorAttributeName:[ColorUtil colorWithHexadecimal:@"#999999"], NSFontAttributeName:FONT(14)} mutableCopy];
        NSMutableAttributedString *attPlace = [[NSMutableAttributedString alloc] initWithString:@"输入商品名称" attributes:attDic];
        _goodsTextField.attributedPlaceholder = attPlace;
//        [_goodsTextField setValue:@(0) forKey:@"_placeholderLabel.numberOfLines"];
        [self.searchView_1 addSubview:_goodsTextField];
        
        _shippingTextField = [[UITextField alloc] init];
        _shippingTextField.delegate = self;
        _shippingTextField.textAlignment = NSTextAlignmentLeft;
        [_shippingTextField addTarget:self action:@selector(shippingTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        _shippingTextField.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        _shippingTextField.keyboardType = UIKeyboardTypeDefault;
        _shippingTextField.placeholder = @"输入仓位号";
        _shippingTextField.clearButtonMode = UITextFieldViewModeAlways;
        NSMutableDictionary *attDic1 = [@{NSForegroundColorAttributeName:[ColorUtil colorWithHexadecimal:@"#999999"], NSFontAttributeName:FONT(14)} mutableCopy];
        NSMutableAttributedString *attPlace1 = [[NSMutableAttributedString alloc] initWithString:@"输入仓位号" attributes:attDic1];
        _shippingTextField.attributedPlaceholder = attPlace1;
        [self.searchView_1 addSubview:_shippingTextField];
        
        _searchView_2 = [[UIView alloc] init];
        _searchView_2.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        _searchView_2.layer.cornerRadius = 5;
        _searchView_2.layer.borderWidth = 1;
        _searchView_2.layer.borderColor = MM_ColorUtil(@"#dddddd").CGColor;
        [self addSubview:_searchView_2];
        
        _searchImageView_2 = [[UIImageView alloc] init];
        _searchImageView_2.image = [UIImage imageNamed:@"searchHomeImage"];
        [self.searchView_2 addSubview:_searchImageView_2];

        _oldCodeTextField = [[UITextField alloc] init];
        _oldCodeTextField.delegate = self;
        _oldCodeTextField.textAlignment = NSTextAlignmentLeft;
        [_oldCodeTextField addTarget:self action:@selector(shippingTextOldCodeFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        _oldCodeTextField.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        _oldCodeTextField.keyboardType = UIKeyboardTypeNamePhonePad;
        _oldCodeTextField.placeholder = @"输入旧编码";
        _oldCodeTextField.clearButtonMode = UITextFieldViewModeAlways;
        NSMutableDictionary *attDic2 = [@{NSForegroundColorAttributeName:[ColorUtil colorWithHexadecimal:@"#999999"], NSFontAttributeName:FONT(14)} mutableCopy];
        NSMutableAttributedString *attPlace2 = [[NSMutableAttributedString alloc] initWithString:@"输入旧编码" attributes:attDic2];
        _oldCodeTextField.attributedPlaceholder = attPlace2;
        [self.searchView_2 addSubview:_oldCodeTextField];

        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn MM_ConfigButtonWithTitle:@"查询" titleFont:FONT_BOLD(16) titleColor:MM_ColorUtil(@"#FFFFFF") bgColor:MM_ColorUtil(@"#1F71C4") cornerRadius:5 borderColor:nil borderWidth:0];
        _searchBtn.tag = 100;
        [_searchBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.searchView_1 addSubview:_searchBtn];
        
        _oldSearchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_oldSearchBtn MM_ConfigButtonWithTitle:@"查询" titleFont:FONT_BOLD(16) titleColor:MM_ColorUtil(@"#FFFFFF") bgColor:MM_ColorUtil(@"#1F71C4") cornerRadius:5 borderColor:nil borderWidth:0];
        _oldSearchBtn.tag = 101;
        [_oldSearchBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.searchView_2 addSubview:_oldSearchBtn];

        [self setUpViews];
    }
    return self;
}

- (void)setUpViews {
    
    [self.searchView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(12);
        make.right.equalTo(-12);
        make.top.equalTo(10);
        make.height.equalTo(40);
    }];
    
    [self.searchImageView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(12);
        make.centerY.equalTo(self.searchView_1);
        make.width.height.equalTo(15);
    }];
    
    [self.goodsTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.searchImageView_1.mas_right).offset(10);
        make.centerY.equalTo(self.searchView_1);
        make.width.equalTo(150);
        make.height.equalTo(40);
    }];
    
    [self.shippingTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(-100);
        make.centerY.equalTo(self.searchView_1);
        make.width.equalTo(100);
        make.height.equalTo(40);
    }];
    
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.centerY.equalTo(self.searchView_1);
        make.width.equalTo(60);
        make.height.equalTo(40);
    }];
    
    [self.searchView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(12);
        make.right.equalTo(-12);
        make.top.equalTo(self.searchView_1.mas_bottom).offset(10);
        make.height.equalTo(40);
    }];
    
    [self.searchImageView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(12);
        make.centerY.equalTo(self.searchView_2);
        make.width.height.equalTo(15);
    }];
    
    [self.oldCodeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.searchImageView_1.mas_right).offset(10);
        make.centerY.equalTo(self.searchView_2);
        make.right.equalTo(72);
        make.height.equalTo(40);
    }];
    
    [self.oldSearchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.centerY.equalTo(self.searchView_2);
        make.width.equalTo(60);
        make.height.equalTo(40);
    }];
}

- (void)goodsTextFieldDidChange:(UITextField *)textField {
    
    if (self.searchGoodsBlock) {
        self.searchGoodsBlock(textField.text);
    }
    
    MMLog(@"searchGoodsBlock(textField.text === %@",textField.text);
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField == self.shippingTextField) {
        
        if (self.searchFirstResponseBlock) {
            self.searchFirstResponseBlock(1);
        }
    }else {
        if (self.searchFirstResponseBlock) {
            self.searchFirstResponseBlock(0);
        }
    }
    return YES;
}

- (void)shippingTextFieldDidChange:(UITextField *)textField {
    
    if (self.searchShippingSpaceBlock) {
        self.searchShippingSpaceBlock(textField.text);
    }
    
    MMLog(@"searchShippingSpaceBlock(textField.text === %@",textField.text);

}

- (void)shippingTextOldCodeFieldDidChange:(UITextField *)textField {
    
    if (self.oldCodeBlock) {
        self.oldCodeBlock(textField.text);
    }
}

- (void)setSearchStr:(NSString *)searchStr {
    _searchStr = searchStr;
    
    self.shippingTextField.text = searchStr;
}

- (void)btnClick:(UIButton *)button {
    
    NSInteger index = button.tag - 100;
    
    if (self.searchInquireBlock) {
        self.searchInquireBlock(index);
    }
}

@end
